package org.example.AES;

import lombok.extern.slf4j.Slf4j;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.util.ClassUtils;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
@Slf4j
public class AESUtil {
    // AES加密
    public static String encrypt(String content, String password) throws Exception {
        // 1. 创建AES密钥
        SecretKeySpec keySpec = new SecretKeySpec(password.getBytes("UTF-8"), "AES");

        // 2. 创建初始化向量（IV），用于AES的CBC模式
        IvParameterSpec ivSpec = new IvParameterSpec(password.getBytes("UTF-8"));

        // 3. 创建Cipher对象，指定加密算法和模式
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

        // 4. 初始化Cipher对象为加密模式
        cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);

        // 5. 加密内容
        byte[] encryptedBytes = cipher.doFinal(content.getBytes("UTF-8"));

        // 6. 将加密后的字节数组转换为Base64字符串，便于存储和传输
        return Base64.getEncoder().encodeToString(encryptedBytes);
    }

    // AES解密
    public static String decrypt(String encryptedContent, String password) throws Exception {
        // 1. 创建AES密钥
        SecretKeySpec keySpec = new SecretKeySpec(password.getBytes("UTF-8"), "AES");

        // 2. 创建初始化向量（IV），用于AES的CBC模式
        IvParameterSpec ivSpec = new IvParameterSpec(password.getBytes("UTF-8"));

        // 3. 创建Cipher对象，指定加密算法和模式
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

        // 4. 初始化Cipher对象为解密模式
        cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);

        // 5. 将Base64字符串解码为字节数组
        byte[] encryptedBytes = Base64.getDecoder().decode(encryptedContent);

        // 6. 解密内容
        byte[] decryptedBytes = cipher.doFinal(encryptedBytes);

        // 7. 将解密后的字节数组转换为字符串
        return new String(decryptedBytes, "UTF-8");
    }
    private static final String DEFAULT_RESOURCE_PATTERN = "**/*.class";
    private static final String CLASSPATH_ALL_URL_PREFIX = "classpath*:";
    private static String resolveBasePackage(String basePackage) {
        return ClassUtils.convertClassNameToResourcePath(new StandardEnvironment().resolveRequiredPlaceholders(basePackage));
    }
    private static ResourcePatternResolver getResourcePatternResolver() {
        return new PathMatchingResourcePatternResolver();
    }
    public static void setString(String... basePackages){
        System.out.println(basePackages.length);
        for(int i=0;i< basePackages.length;i++){
            String string = basePackages[i];
            System.out.println(string);
        }

        List<Class<?>> cls = new ArrayList<>();
        for (String basePackage : basePackages) {
            try {
                String packageSearchPath = CLASSPATH_ALL_URL_PREFIX + resolveBasePackage(basePackage) + '/' + DEFAULT_RESOURCE_PATTERN;
                Resource[] resources = getResourcePatternResolver().getResources(packageSearchPath);
                String basePackage0 = basePackage.replace(".", "/");
                for (Resource resource : resources) {
                    String path = resource.getURI().toString();
                    if (path.indexOf(basePackage0) > 0) {
                        String className = path.substring(path.indexOf(basePackage0)).split("\\.")[0].replace("/", ".");
                        Class class0 = ClassUtils.getDefaultClassLoader().loadClass(className);
                        if (class0.isAnnotationPresent(WsController.class)) {
                            cls.add(class0);
                        }
                    }
                }
            } catch (Exception e) {
                log.info("ws在扫描包:" + basePackage + "出错", e);
            }
        }
    }

    public static void main(String[] args) {
//        try {
//            String content = "Hello, AES Encryption!";
//            String password = "1234567890123456"; // 密钥长度必须为16、24或32字节
//
//            // 加密
//            String encryptedContent = encrypt(content, password);
//            System.out.println("加密 Encrypted Content: " + encryptedContent);
//
//            // 解密
//            String decryptedContent = decrypt(encryptedContent, password);
//            System.out.println("解密 Decrypted Content: " + decryptedContent);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }

        setString("com.cpic.sxwx.healthy.ws.internal","com.cpic.sxwx.healthy.ws.business.handler.quizpk");
    }
}